<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告名字" prop="advertisingName">
        <el-input
          v-model="queryParams.advertisingName"
          placeholder="请输入广告名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="advertisingState">
        <el-select v-model="queryParams.advertisingState" placeholder="请选择" clearable size="small" >
          <el-option label="请选择" value="" />
          <el-option label="已展示" value="0" />
          <el-option label="未展示" value="1" />


        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="advertisingType">
        <el-select v-model="queryParams.advertisingType" placeholder="请选择" clearable size="small" >
          <el-option label="请选择" value="" />
          <el-option label="静态广告" value="1" />
          <el-option label="外部广告" value="2" />
          <el-option label="视频广告" value="3" />
          <el-option label="综艺视频广告" value="4" />


        </el-select>
      </el-form-item>
      <el-form-item label="视频分类" prop="videoCategoryId">
        <el-select v-model="queryParams.videoCategoryId" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in options1"
            :key="item.spVideoCategory.videoCategoryId"
            :label="item.spVideoCategory.videoCategoryName"
            :value="item.spVideoCategory.videoCategoryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="banner图分类" prop="advertisingType">
        <el-select v-model="queryParams.bannerType" placeholder="请选择banner图分类" clearable size="small" >
          <el-option label="请选择banner图分类" value="" />
          <el-option label="首页" :value="1" />
          <el-option label="楼凤" :value="2" />
          <el-option label="漫画" :value="3" />
          <el-option label="动漫" :value="4" />


        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xqsp:advertising:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['xqsp:advertising:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xqsp:advertising:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:advertising:export']"
        >导出</el-button>
      </el-col>

       <el-col :span="1.5">
                          <el-button
                            type="info"
                            plain
                            icon="el-icon-bottom"
                            size="mini"
                            @click="handleClick"

                          >排序</el-button>
                        </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advertisingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告编号" align="center" prop="spAdvertising.advertisingId" />
      <el-table-column label="广告名字" align="center" prop="spAdvertising.advertisingName" />
      <el-table-column label="点击次数" align="center" prop="spAdvertising.advertisingClick" />
      <el-table-column label="广告图片" align="center" >
              <template slot-scope="scope">
                <el-image :src="scope.row.spAdvertising.advertisingImg"  :preview-src-list="[scope.row.spAdvertising.advertisingImg]"  style="width: 100px;height: 100px"/>
              </template>
            </el-table-column>

      <el-table-column label=" 广告链接" align="center" prop="spAdvertising.advertisingLink" />
      <el-table-column label="广告状态 " align="center" prop="spAdvertising.advertisingState">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spAdvertising.advertisingState"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)">

          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="精选页banner图" align="center" prop="spAdvertising.advertisingHome">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spAdvertising.advertisingHome"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleHomeChange(scope.row)">

          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="视频链接名" align="center" prop="videoName" />
      <el-table-column label="类型" align="center" prop="spAdvertising.advertisingType" />
      <el-table-column label="视频分类" align="center" prop="categoryName" />
      <el-table-column label="banner图分类" align="center" prop="spAdvertising.bannerType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:advertising:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:advertising:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告位管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告名字" prop="advertisingName">
          <el-input v-model="form.advertisingName" placeholder="请输入广告名字" />
        </el-form-item>
        <el-form-item label="点击次数" prop="advertisingClick">
          <el-input v-model="form.advertisingClick" placeholder="请输入点击次数(默认为0)" />
        </el-form-item>
        <el-form-item label="广告图片" prop="advertisingImg">
          <el-input v-model="form.advertisingImg" type="textarea" placeholder="请输入广告图片地址" />
        </el-form-item>
        <el-form-item label="banner图分类" prop="bannerType">
          <el-select v-model="form.bannerType" placeholder="请选择banner图分类">
            <el-option label="请选择" value="" />
            <el-option label="首页" :value="1" />
            <el-option label="楼凤" :value="2" />
            <el-option label="漫画" :value="3" />
            <el-option label="动漫" :value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="类型">
        <template>

          <el-popover
            placement="bottom-start"
            width="400"
            trigger="focus"
            @show="show4"
          >
            <el-button slot="reference" size="medium" round>静态广告</el-button>
          </el-popover>

          <el-popover
            placement="bottom-start"
            width="400"
            trigger="click"
            @show="show3"
          >
            <el-form-item label=" 广告链接" prop="advertisingLink">
              <el-input v-model="form.advertisingLink" placeholder="请输入 广告链接" />
              <el-input type="hidden" v-model="form.advertisingType"  />
            </el-form-item>
            <el-button slot="reference" size="medium" round>外部广告</el-button>
          </el-popover>

          <el-popover
            placement="bottom-start"
            width="400"
            trigger="click"
            @show="show2"
          v-show="this.form.bannerType==1">
            <el-form-item label="视频id" prop="advertisingTypeId">
              <el-select v-model="form.advertisingTypeId" clearable filterable placeholder="请选择视频">
                <el-option
                  v-for="item in options"
                  :key="item.spVideo.videoId"
                  :label="item.spVideo.videoName"
                  :value="item.spVideo.videoId">
                </el-option>
              </el-select>
              <el-input type="hidden" v-model="form.advertisingType" />
            </el-form-item>
            <el-button slot="reference" size="medium" round>视频推广</el-button>
          </el-popover>

          <el-popover
            placement="bottom-start"
            width="400"
            trigger="click"
            @show="show1"
            v-show="this.form.bannerType==1"
          >
            <el-form-item label="综艺视频" prop="advertisingTypeId">
              <el-select v-model="form.advertisingTypeId" filterable clearable placeholder="请选择综艺视频">
                <el-option
                  v-for="item in options2"
                  :key="item.spVarietyVideo.varietyVideoId"
                  :label="item.spVarietyVideo.varietyVideoName"
                  :value="item.spVarietyVideo.varietyVideoId">
                </el-option>
              </el-select>
              <el-input type="hidden" v-model="form.advertisingType" />
            </el-form-item>

            <el-button slot="reference" size="medium" round>综艺视频推广</el-button>
          </el-popover>
        </template>
        </el-form-item>
        <el-form-item label="类型" prop="advertisingType">
          <el-select v-model="form.advertisingType" clearable filterable placeholder="请选择" disabled>
            <el-option label="请选择" value="" />
            <el-option label="静态广告" :value="1" />
            <el-option label="外部广告" :value="2" />
            <el-option label="视频广告" :value="3" />
            <el-option label="综艺视频广告" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="视频分类id" prop="videoCategoryId"   v-show="this.form.bannerType==1">
          <el-select v-model="form.videoCategoryId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in options1"
              :key="item.spVideoCategory.videoCategoryId"
              :label="item.spVideoCategory.videoCategoryName"
              :value="item.spVideoCategory.videoCategoryId">
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdvertising, getAdvertising, delAdvertising, addAdvertising, updateAdvertising, exportAdvertising ,changeBannerStatus ,changeBannerHome } from "@/api/xqsp/advertising";
import { listCategory } from "@/api/xqsp/category";
import { listVideo} from "@/api/xqsp/video";
import { listVarietyVideo} from "@/api/xqsp/varietyVideo";
export default {
  name: "Advertising",
  components: {

  },
  data() {
    return {
      //视频分类下拉框
      options1:[],
      options:[],
      options2:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告位管理表格数据
      advertisingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        advertisingName: null,
        advertisingClick: null,
        advertisingImg: null,
        advertisingLink: null,
        advertisingState: null,
        advertisingTypeId: null,
        advertisingType: null,
        videoCategoryId: null,
        advertisingHome: null,
        bannerType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        advertisingName: [
          {required: true, message: "广告名称不能为空", trigger: "blur"}
        ],
        advertisingClick: [
          {
            pattern: /^[+]{0,1}(\d+)$/,
            message: '请输入正整数',
            trigger: 'blur'
          }
        ],
        advertisingImg: [
          {required: true, message: "图片链接不能为空", trigger: "blur"}
        ],

        bannerType: [
          {required: true, message: "banner图分类不能为空", trigger: "blur"}
        ],


      }
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
    this.getVideoList();
    this.getVarietyVideoList();

  },
  methods: {

    /** 查询综艺视频*/
    getVarietyVideoList() {
      listVarietyVideo().then(response => {
        this.options2 = response.rows;

      });
    },

    //查询视频
    getVideoList() {
      listVideo(this.formData).then(response => {

          this.options =  response.rows;



      });
    },


    //查询分类
    getCategoryList(){
      listCategory().then(response => {

        this.options1 = response.rows;
        this.options1.forEach((item, index) => {
          if (item.spVideoCategory.videoCategoryId === 9) {
            this.options1.splice(index, 1)
          }
          if (item.spVideoCategory.videoCategoryId === 11) {
            this.options1.splice(index, 1)
          }

        });
        this.options1.forEach((item, index) => {
          if (item.spVideoCategory.videoCategoryId === 10) {
            this.options1.splice(index, 1)
          }


        });
      });
    },

    //给类型赋值
    show1(){
     this.form.advertisingType=4
    },
    show2(){
      this.form.advertisingType=3
    },
    show3(){
      this.form.advertisingType=2
    },
    show4(){
      this.form.advertisingType=1
    },

    /** 查询广告位管理列表 */
    getList() {
      this.loading = true;
      listAdvertising(this.queryParams).then(response => {
        this.advertisingList = response.rows;

        this.advertisingList.forEach(item=>{
          if (item.spAdvertising.advertisingType==1){
            item.spAdvertising.advertisingType="banner图"
          }else if (item.spAdvertising.advertisingType==2){
            item.spAdvertising.advertisingType="外部广告"
          }else if (item.spAdvertising.advertisingType==3){
            item.spAdvertising.advertisingType="视频广告"
          }
          else if (item.spAdvertising.advertisingType==4){
            item.spAdvertising.advertisingType="综艺视频广告"
          }
          if (item.spAdvertising.bannerType==1){
            item.spAdvertising.bannerType="首页"
          }else if (item.spAdvertising.bannerType==2){
            item.spAdvertising.bannerType="楼凤"
          }else if (item.spAdvertising.bannerType==3){
            item.spAdvertising.bannerType="漫画"
          }
          else if (item.spAdvertising.bannerType==4){
            item.spAdvertising.bannerType="动漫"
          }
        });




        this.total = response.total;
        this.loading = false;
      });
    },
     /** 根据点击次数排序 */
        getSortList() {
          this.loading = true;
          listAdvertising(this.queryParams).then(response => {
            this.advertisingList = response.rows.sort( function(a,b){
                                                                  return b.spAdvertising.advertisingClick-a.spAdvertising.advertisingClick;
                                                                  });

            this.advertisingList.forEach(item=>{
              if (item.spAdvertising.advertisingType==1){
                item.spAdvertising.advertisingType="banner图"
              }else if (item.spAdvertising.advertisingType==2){
                item.spAdvertising.advertisingType="外部广告"
              }else if (item.spAdvertising.advertisingType==3){
                item.spAdvertising.advertisingType="视频广告"
              }
              else if (item.spAdvertising.advertisingType==4){
                item.spAdvertising.advertisingType="综艺视频广告"
              }
              if (item.spAdvertising.bannerType==1){
                item.spAdvertising.bannerType="首页"
              }else if (item.spAdvertising.bannerType==2){
                item.spAdvertising.bannerType="楼凤"
              }else if (item.spAdvertising.bannerType==3){
                item.spAdvertising.bannerType="漫画"
              }
              else if (item.spAdvertising.bannerType==4){
                item.spAdvertising.bannerType="动漫"
              }
            });


            this.total = response.total;
            this.loading = false;
          });
        },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        advertisingId: null,
        advertisingName: null,
        advertisingClick: null,
        advertisingImg: null,
        advertisingLink: null,
        advertisingState: null,
        advertisingTypeId: null,
        advertisingType: null,
        videoCategoryId: null,
        advertisingHome: null,
        bannerType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**根据点击次数查询*/
    handleClick(){
    this.getSortList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spAdvertising.advertisingId)
      this.names = selection.map(item => item.spAdvertising.advertisingName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const advertisingId = row.spAdvertising.advertisingId
      const advertisingName = row.spAdvertising.advertisingName

      getAdvertising(advertisingId).then(response => {
        console.log(response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改“"+advertisingName+"”广告位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bannerType!=1){
            this.form.advertisingTypeId=null;
          }
          if (this.form.bannerType!=1){
            this.form.videoCategoryId=null;
          }
          if (this.form.advertisingId != null) {
            updateAdvertising(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdvertising(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let tmpIds=[]
      if (this.ids.length>0){
        for (const id of this.ids) {
          tmpIds.push(id);
        }
      }else {
        tmpIds.push(row.spAdvertising.advertisingId);
      }


      this.$confirm('是否确认删除广告位管理编号为"' + tmpIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAdvertising(tmpIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有广告位管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAdvertising(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 是否展示
    handleStatusChange(row) {
      let text = row.spAdvertising.advertisingState === 0 ? "展示" : "不展示";
      this.$confirm('确认要"' + text + '"编号为"' + row.spAdvertising.advertisingId + '"的banner图吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeBannerStatus(row.spAdvertising.advertisingId, row.spAdvertising.advertisingState);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.spAdvertising.advertisingState = row.spAdvertising.advertisingState === 0 ? 1 : 0;
      });
    },

    // 是否展示
    handleHomeChange(row) {
      let text = row.spAdvertising.advertisingHome === 0 ? "展示" : "不展示";
      this.$confirm('确认要"' + text + '"编号为"' + row.spAdvertising.advertisingId + '"的banner图吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeBannerHome(row.spAdvertising.advertisingId, row.spAdvertising.advertisingHome);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.spAdvertising.advertisingHome = row.spAdvertising.advertisingHome === 0 ? 1 : 0;
      });
    },
  }
};
</script>
